# 基于debian:bookworm-slim镜像
FROM debian:bookworm-slim

# 设置环境变量
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
ENV LANG=C.UTF-8
ENV LC_ALL=C.UTF-8

# 设置代理
ENV HTTP_PROXY=http://192.168.10.4:10809
ENV HTTPS_PROXY=http://192.168.10.4:10809
ENV http_proxy=http://192.168.10.4:10809
ENV https_proxy=http://192.168.10.4:10809

# 配置镜像源（使用HTTP避免证书问题）
RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources && \
    sed -i 's|http://security.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources

# 先安装ca-certificates
RUN apt-get update && apt-get install -y ca-certificates && \
    sed -i 's|http://mirrors.aliyun.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources && \
    apt-get update

# 安装基础工具
RUN apt-get install -y \
    curl \
    wget \
    vim \
    git \
    build-essential \
    software-properties-common \
    gnupg \
    lsb-release \
    apt-transport-https \
    && rm -rf /var/lib/apt/lists/*

# 安装PostgreSQL 15
RUN apt-get update && apt-get install -y \
    postgresql-15 \
    postgresql-client-15 \
    postgresql-contrib-15 \
    && rm -rf /var/lib/apt/lists/*

# 安装Redis 7
RUN apt-get update && apt-get install -y \
    redis \
    && rm -rf /var/lib/apt/lists/*

# 安装Python 3.11
RUN apt-get update && apt-get install -y \
    python3.11 \
    python3.11-dev \
    python3.11-venv \
    python3.11-distutils \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

# 创建Python虚拟环境
RUN python3.11 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# 升级pip并配置清华源
RUN pip install --upgrade pip
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

# 安装Node.js 18
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
    apt-get install -y nodejs && \
    rm -rf /var/lib/apt/lists/*

# 配置npm淘宝镜像源
RUN npm config set registry https://registry.npmmirror.com

# 创建项目目录结构
RUN mkdir -p /app/{backend,frontend,config,logs}

# 设置工作目录
WORKDIR /app

# 复制配置文件模板
COPY config/ /app/config/
COPY scripts/ /app/scripts/
COPY .env.example /app/.env

# 设置权限
RUN chmod +x /app/scripts/*.sh

# 暴露端口
EXPOSE 8080 7001 5432 6379

# 创建启动脚本
RUN echo '#!/bin/bash\n\
service postgresql start\n\
service redis-server start\n\
exec "$@"' > /app/entrypoint.sh && \
chmod +x /app/entrypoint.sh

# 设置入口点
ENTRYPOINT ["/app/entrypoint.sh"]

# 默认命令
CMD ["/bin/bash"]